package com.cloud.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pojo.EventWarn;
import com.cloud.result.Result;
import com.cloud.service.EventWarnService;
import com.cloud.utils.GsonBuilderUtil;
import com.google.gson.Gson;

@Controller
@RequestMapping(value="event")
public class EventWarnController {
	
	@Autowired
	private EventWarnService eventWarnService;
	
	/**
	 * 添加事件提醒
	 * @param eventWarn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add" ,method = RequestMethod.POST)
	public String addEventWarn(HttpServletRequest request,EventWarn eventWarn){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		if(eventWarn != null){
			if(StringUtils.isNotBlank(eventWarn.getContent())){
				try {
					String adminId_Str = null;
					Cookie[] cookies = request.getCookies();
					 if(cookies!=null&&cookies.length>0){   
					        //遍历Cookie  
					        for(int i=0;i<cookies.length;i++){  
					            Cookie cookie=cookies[i];  
					            //此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
					            if("id".equals(cookie.getName())){  
					            	adminId_Str = cookie.getValue();  
					            }  
					        }  
					    }
				//获取userid,存入对象
				Integer userId = Integer.valueOf(adminId_Str);
				eventWarn.setUserId(userId);
				eventWarn.setCreateTime(new Date());
				Integer id = eventWarnService.addEventWarn(eventWarn);
				if(id > 0 ){
					result.setSuccess(id);
				}else{
					result.setFailure(-1, "添加备忘内容失败");
				}
				}catch(Exception e){
					result.setException(e);
				}
			}else {
				result.setFailure(1, "请输入备忘内容!");
			}
		}else{
			result.setException("参数传递异常");
		}
		return gson.toJson(result);
	}
	
	/**
	 * 删除一条备忘内容
	 * @param eventId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete" ,method = RequestMethod.GET)
	public String deleteEventWarnById(HttpServletRequest request,Integer eventId){
		Gson gson = GsonBuilderUtil.create();
		Result<Integer> result = new Result<>();
		if(eventId != null){
			try {
				eventWarnService.deleteEventWarnById(eventId);
				result.setSuccess(null);
			} catch (Exception e) {
				result.setException(e);
			}
		}
		return gson.toJson(result);
	}
	
	/**
	 * 获取所有的备忘录
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list" ,method = RequestMethod.GET)
	public String getEventWarns(HttpServletRequest request){
		Gson gson = GsonBuilderUtil.create();
		Result<List<EventWarn>> result = new Result<>();
		try {
			String adminId_Str = null;
			Cookie[] cookies = request.getCookies();
			 if(cookies!=null&&cookies.length>0){   
			        //遍历Cookie  
			        for(int i=0;i<cookies.length;i++){  
			            Cookie cookie=cookies[i];  
			            //此处类似与Map有name和value两个字段,name相等才赋值,并处理编码问题   
			            if("id".equals(cookie.getName())){  
			            	adminId_Str = cookie.getValue();  
			            }  
			        }  
			    }
			//获取userid,存入对象
			Integer userId = Integer.valueOf(adminId_Str);
			
			List<EventWarn> list = eventWarnService.getEventWarns(userId);
			result.setSuccess(list);
		} catch (Exception e) {
			result.setException(e);
		}
		return gson.toJson(result);
	}
	
	
	
}
